package com.hkts.reportform.datastatics.serviceImpl;

import com.hkts.reportform.datastatics.dao.CustomSearchDao;
import com.hkts.reportform.datastatics.dao.ReportDao;
import com.hkts.reportform.datastatics.entity.ReportForm;
import com.hkts.reportform.datastatics.service.WechatDataService;
import com.hkts.reportform.utils.DateUtil;
import com.hkts.reportform.utils.ErpProperties;
import com.hkts.reportform.utils.InventoryClassFildes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassNameWechatDataServiceImpl
 * @Description
 * @Author yck
 * @Date2020-10-30 14:22
 * @Version V1.0
 **/
@Service("wechatDataServiceImpl")
public class WechatDataServiceImpl implements WechatDataService {
    @Autowired
    private CustomSearchDao customSearchDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ReportDao reportDaoImpl;

    //石油焦消耗专业业务
    @Override
    public Map<String, Object> cusomeFindStockByCode(String dateType, String beginDate, String endDate, String cInvCCode) {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> mapResult = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        List<String> dateArray = new ArrayList<String>();
        List<String> nameArray = new ArrayList<String>();
        List<Float> valueArray = new ArrayList<Float>();
        SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd");
        dateArray = getDateArray(dateType, beginDate, endDate);
        List<Map<String, Object>> maps1 = reportDaoImpl.selectInventoryClassBycInvCCode(cInvCCode);
        List<Map<String, Object>> maps = customSearchDao.cusomeFindStockByCode(dateArray.get(0), dateArray.get(dateArray.size() - 1), cInvCCode);
        for (int i = 0; i < maps.size(); i++) {
            if (Float.parseFloat(maps.get(i).get("iquantity").toString()) > 1) {
                valueArray.add(Float.parseFloat(maps.get(i).get("iquantity").toString()));
                nameArray.add(maps.get(i).get("cInvName").toString());
            }
        }
        mapResult.put("name", maps1.get(0).get("cInvCName").toString());//cInvCName -> 石油焦
        mapResult.put("data", valueArray);
        map.put("series", mapResult);
        map.put("categories", nameArray);
        return map;
    }

    @Override
    public Map<String, Object> consumeData(String dateType, String beginDate, String endDate, String cInvCCode) throws ParseException {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> mapResult = new HashMap<>();
        List<String> dateArray = new ArrayList<String>();
        SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd");
        dateArray = getDateArray(dateType, beginDate, endDate);
        List<Map<String, Object>> maps = customSearchDao.cusomeFindStock(dateArray.get(0), dateArray.get(dateArray.size() - 1), cInvCCode);
        List<String> names = new ArrayList<>();
        Map<String, Map<String, Object>> map = new HashMap<>();
        int temp = 0;
        for (int g = 0; g < maps.size(); g++) {
            if (!map.containsKey(maps.get(g).get("cInvCode"))) {
                Map<String, Object> valueMap = new HashMap<String, Object>();
                List<String> dates = new ArrayList<>();
                List<String> valuess = new ArrayList<>();
                dates.add(maps.get(g).get("dDate").toString());
                valuess.add(maps.get(g).get("iquantity").toString());
                valueMap.put("dDate", dates);
                valueMap.put("cInvName", maps.get(g).get("cInvName"));
                valueMap.put("iquantity", valuess);
                map.put(maps.get(g).get("cInvCode").toString(), valueMap);
            } else {
                Map<String, Object> valueMap = map.get(maps.get(g).get("cInvCode"));
                List<String> dDate = (List<String>) valueMap.get("dDate");
                List<String> iquantity = (List<String>) valueMap.get("iquantity");
                dDate.add(maps.get(g).get("dDate").toString());
                iquantity.add(maps.get(g).get("iquantity").toString());
                valueMap.put("iquantity", iquantity);
                valueMap.put("dDate", dDate);
                map.put(maps.get(g).get("cInvCode").toString(), valueMap);
            }
        }
        for (Map.Entry<String, Map<String, Object>> entry : map.entrySet()) {
            Map<String, Object> result = new HashMap<>();
            String mapKey = entry.getKey();
            Map<String, Object> mapValue = entry.getValue();
            List<String> dDate = (List<String>) mapValue.get("dDate");
            List<String> iquantity = (List<String>) mapValue.get("iquantity");
            result.put("name", mapValue.get("cInvName"));
            List<Float> valuess = new ArrayList<>();
            Float zero = (float) 0;
            for (int i = 0; i < dateArray.size(); i++) {
                if (dDate.contains(dateArray.get(i))) {
                    valuess.add(Float.parseFloat(iquantity.get(dDate.indexOf(dateArray.get(i))).toString()));
                } else {
                    valuess.add(zero);
                }
            }
            temp = 0;
            for (int i = 0; i < valuess.size(); i++) {
                if (valuess.get(i) > 1) {
                    result.put("data", valuess);
                    list.add(result);
                    break;
                }
            }
            result.put("data", valuess);
            list.add(result);
        }
        mapResult.put("dateArray",DateUtil.dateListConvert(dateArray));
        mapResult.put("data", list);
        return mapResult;
    }

    //消耗库存修改版
    @Override
    public Map<String, Object> consumeDataUpdate(String dateType, String beginDate, String endDate, String cInvCCode) throws ParseException {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> mapResult = new HashMap<>();
        List<String> dateArray = new ArrayList<String>();
        SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd");
        dateArray = getDateArray(dateType, beginDate, endDate);
        List<Map<String, Object>> maps = customSearchDao.cusomeFindStockUpdate(dateArray.get(0), dateArray.get(dateArray.size() - 1), cInvCCode);
        List<Map<String, Object>> maps1 = reportDaoImpl.selectInventoryClassBycInvCCode(cInvCCode);
        List<String> names = new ArrayList<>();
        List<Float> floatArray = new ArrayList<Float>();
        List<Float> valueArray = new ArrayList<Float>();
        Map<String, Object> map = new HashMap<>();
        for (int i = 0; i < maps.size(); i++) {

            if (Float.parseFloat(maps.get(i).get("iquantity").toString()) > 0) {
                names.add(maps.get(i).get("dDate").toString());
                floatArray.add(Float.parseFloat(maps.get(i).get("iquantity").toString()));
            } else {
                names.add(maps.get(i).get("dDate").toString());
                floatArray.add((float) 0);
            }
        }
        for (int i = 0; i < dateArray.size(); i++) {
            if (names.contains(dateArray.get(i))) {
                valueArray.add(floatArray.get(names.indexOf(dateArray.get(i))));
            } else {
                valueArray.add((float) 0);
            }
        }
        map.put("data", valueArray);
        map.put("name", maps1.get(0).get("cInvCName").toString());//cInvCName -> 生坯
        mapResult.put("categories",DateUtil.dateListConvert(dateArray) );
        list.add(map);
        mapResult.put("series", map);
        return mapResult;
    }

    @Override
    public Map<String, Object> finishedProductsDataUpdate(String dateType, String beginDate, String endDate, String cInvCodeS) throws Exception {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> mapResult = new HashMap<>();
        List<String> dateArray = new ArrayList<String>();
        SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd");
        dateArray = getDateArray(dateType, beginDate, endDate);
        List<Map<String, Object>> maps = customSearchDao.cusomeFindStockUpdate(dateArray.get(0), dateArray.get(dateArray.size() - 1), cInvCodeS);
        List<Map<String, Object>> inventoryClass = reportDaoImpl.selectInventoryClassBycInvCCode(cInvCodeS);
        List<String> names = new ArrayList<>();
        List<Float> values = new ArrayList<>();
        Map<String, Object> mm = new HashMap<>();
        Map<String, Object> result = new HashMap<>();
        //if(cInvCodeS.equals("0301")){
        List<ReportForm> storedProcedure = reportDaoImpl.getStoredProcedure(dateArray.get(0), dateArray.get(dateArray.size() - 1), cInvCodeS);
        for (int i = 0; i < storedProcedure.size(); i++) {
            if (storedProcedure.get(i).getYield() > 1) {
                names.add(storedProcedure.get(i).getStockName());
                values.add(storedProcedure.get(i).getYield());
            }
        }
        result.put("categories", names);
        //series
        mm.put("name", inventoryClass.get(0).get("cInvCName"));
        mm.put("data", values);
        result.put("series", mm);
        return result;
    }

    //现存量数据分析
    @Override
    public Map<String, Object> getStockData(String dateType, String beginDate, String endDate, String inventoryClass) throws ParseException {
        List<Map<String, Object>> list = reportDaoImpl.selectInventoryByCInvCCode(inventoryClass);
        List<String> dateArray = getDateArray(dateType, beginDate, endDate);
        int temp = 0;
        Map<String, Object> result = new HashMap<>();
        List<Map<String, Object>> resultlist = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {

            String cInvName = list.get(i).get("cInvName").toString();
            List<Float> valueList = new ArrayList<>();
            for (int j = 0; j < dateArray.size(); j++) {
                Object cInvCode = redisTemplate.opsForHash().get(ErpProperties.WECHAT_STOCK + dateArray.get(j), list.get(i).get("cInvCode"));
                if (cInvCode == null) {
                    valueList.add((float) 0);
                } else {
                    Float value = Float.parseFloat(redisTemplate.opsForHash().get(ErpProperties.WECHAT_STOCK + dateArray.get(j), list.get(i).get("cInvCode")).toString());
                    valueList.add(value);
                }

            }
            //判断该存货数据是否都为0如果是则不赋值
            temp = 0;
            for (int j = 0; j < valueList.size(); j++) {
                if (valueList.get(j) > 0) {
                    temp = 1;
                    break;
                }
            }
            if (temp == 1) {
                Map<String, Object> resultItem = new HashMap<>();
                resultItem.put("name", cInvName);
                resultItem.put("data", valueList);
                resultlist.add(resultItem);
            }
        }
        result.put("categories",DateUtil.dateListConvert(dateArray) );
        result.put("series", resultlist);
        return result;
    }

    @Override
    public List<Map<String, Object>> getReltimeStockData(String inventoryClass) throws Exception {
        SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd");
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        List<ReportForm> storedProcedure = reportDaoImpl.getStoredProcedure(formate.format(new Date()), formate.format(new Date()), inventoryClass);
        for (int i = 0; i < storedProcedure.size(); i++) {
            if (storedProcedure.get(i).getStockOnHand() > 1) {
                Map<String, Object> map = new HashMap<>();
                map.put("name", storedProcedure.get(i).getStockName());
                map.put("data", storedProcedure.get(i).getStockOnHand());
                list.add(map);
            }
        }
        return list;
    }


    //生产情况
    @Override
    public Map<String, Object> finishedProductsData(String dateType, String beginDate, String endDate, String cInvCCode) throws ParseException {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> mapResult = new HashMap<>();
        List<String> dateArray = new ArrayList<String>();
        SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd");
        dateArray = getDateArray(dateType, beginDate, endDate);
        List<Map<String, Object>> maps = customSearchDao.finishedProductsData(dateArray.get(0), dateArray.get(dateArray.size() - 1), cInvCCode);
        List<String> names = new ArrayList<>();
        Map<String, Map<String, Object>> map = new HashMap<>();
        for (int g = 0; g < maps.size(); g++) {
            if (!map.containsKey(maps.get(g).get("cInvCode"))) {
                Map<String, Object> valueMap = new HashMap<String, Object>();
                List<String> dates = new ArrayList<>();
                List<String> valuess = new ArrayList<>();
                dates.add(maps.get(g).get("dDate").toString());
                valuess.add(maps.get(g).get("iquantity").toString());
                valueMap.put("dDate", dates);
                valueMap.put("cInvName", maps.get(g).get("cInvName"));
                valueMap.put("iquantity", valuess);
                map.put(maps.get(g).get("cInvCode").toString(), valueMap);
            } else {
                Map<String, Object> valueMap = map.get(maps.get(g).get("cInvCode"));
                List<String> dDate = (List<String>) valueMap.get("dDate");
                List<String> iquantity = (List<String>) valueMap.get("iquantity");
                dDate.add(maps.get(g).get("dDate").toString());
                iquantity.add(maps.get(g).get("iquantity").toString());
                valueMap.put("iquantity", iquantity);
                valueMap.put("dDate", dDate);
                map.put(maps.get(g).get("cInvCode").toString(), valueMap);
            }
        }
        int temp = 0;
        for (Map.Entry<String, Map<String, Object>> entry : map.entrySet()) {
            Map<String, Object> result = new HashMap<>();
            String mapKey = entry.getKey();
            Map<String, Object> mapValue = entry.getValue();
            List<String> dDate = (List<String>) mapValue.get("dDate");
            List<String> iquantity = (List<String>) mapValue.get("iquantity");
            result.put("name", mapValue.get("cInvName"));
            List<Float> valuess = new ArrayList<>();
            Float zero = (float) 0;
            for (int i = 0; i < dateArray.size(); i++) {
                if (dDate.contains(dateArray.get(i))) {
                    valuess.add(Float.parseFloat(iquantity.get(dDate.indexOf(dateArray.get(i))).toString()));
                } else {
                    valuess.add(zero);
                }
            }
            temp = 0;
            for (int i = 0; i < valuess.size(); i++) {
                if (valuess.get(i) > 0) {
                    result.put("data", valuess);
                    list.add(result);
                    break;
                }
            }

        }
        mapResult.put("categories", DateUtil.dateListConvert(dateArray));
        mapResult.put("series", list);
        return mapResult;
    }


    @Override
    public Map<String, Object> inventoryCusomeByCInvCode() {
        Map<String, Object> result = new HashMap<>();
        //石油焦消耗量
        List<String> week = getDateArray("week", "", "");
        List<String> month = getDateArray("month", "", "");
        Float petrolumWeek = customSearchDao.inventoryCusomeByCInvCode(week.get(0), week.get(week.size() - 1), InventoryClassFildes.PETROLEUM_COKE);
        result.put("petrolumWeek", petrolumWeek);
        Float petrolumMonth = customSearchDao.inventoryCusomeByCInvCode(month.get(0), month.get(month.size() - 1), InventoryClassFildes.PETROLEUM_COKE);
        result.put("petrolumMonth", petrolumMonth);
        //生坯产量
        Float greenBodyWeek = customSearchDao.inventoryeProductByCInvCode(week.get(0), week.get(week.size() - 1), InventoryClassFildes.GREEN_BODY);
        Float greenBodyMonth = customSearchDao.inventoryeProductByCInvCode(month.get(0), month.get(month.size() - 1), InventoryClassFildes.GREEN_BODY);
        result.put("greenBodyWeek", greenBodyWeek);
        result.put("greenBodyMonth", greenBodyMonth);
        //熟坯产量
        Float cookedBilletWeek = customSearchDao.inventoryeProductByCInvCode(week.get(0), week.get(week.size() - 1), InventoryClassFildes.COOKED_BILLET);
        Float cookedBilletMonth = customSearchDao.inventoryeProductByCInvCode(month.get(0), month.get(month.size() - 1), InventoryClassFildes.COOKED_BILLET);
        result.put("cookedBilletWeek", cookedBilletWeek);
        result.put("cookedBilletMonth", cookedBilletMonth);
        return result;
    }

    @Override
    public Map<String, Object> accessoriesSumByDate(String dateType, String beginDate, String endDate) throws ParseException {
        Map<String, Object> result = new HashMap<>();
        List<String> dateArray = getDateArray(dateType, beginDate, endDate);
        List<Map<String, Object>> maps = new ArrayList<>();
        Map<String, Object> resultItem = new HashMap<>();
        resultItem.put("name", "出库数量");
        List<Float> listValue = new ArrayList<>();
        List<Map<String, Object>> list = customSearchDao.accessoriesSumByDate(dateArray.get(0), dateArray.get(dateArray.size() - 1));
        for (int i = 0; i < list.size(); i++) {
            float iquantity = Float.parseFloat(list.get(i).get("iquantity").toString());
            listValue.add(iquantity);
        }
        resultItem.put("data", listValue);
        result.put("categories",DateUtil.dateListConvert(dateArray) );
        maps.add(resultItem);
        result.put("series", resultItem);
        return result;
    }

    @Override
    public List<Map<String, Object>> accessoriesTopTen(String dateType, String beginDate, String endDate) {
        List<String> dateArray = getDateArray(dateType, beginDate, endDate);
        List<Map<String, Object>> maps = customSearchDao.accessoriesTopTen(dateArray.get(0), dateArray.get(dateArray.size() - 1));
        return maps;
    }

    public List<String> getDateArray(String dateType, String beginDate, String endDate) {
        SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formateMonth = new SimpleDateFormat("yyyy-MM");
        List<String> dateArray = new ArrayList<String>();
        if (dateType.equals("yesterday")) {
            String format = formate.format(DateUtil.getYesterday());
            dateArray.add(format);
            dateArray.add(format);
        } else if (dateType.equals("beforYesterday")) {
            String format = formate.format(DateUtil.getYesterday(2));
            dateArray.add(format);
            dateArray.add(format);
        }else if (dateType.equals("week")) {
            dateArray = DateUtil.getDateArray(7);
        } else if (dateType.equals("month")) {
            endDate = formate.format(DateUtil.getYesterday());
            beginDate = formateMonth.format(new Date()) + "-01";
            String[] array = {beginDate, endDate};
            dateArray = DateUtil.getDaysByTime(array);
        } else {
            String[] array = {beginDate, endDate};
            dateArray = DateUtil.getDaysByTime(array);
        }
        return dateArray;
    }

    //首页生产情况
    @Override
    public Map<String, Object> getIndexProduct(String dateType, String inventoryClass) throws Exception {
        String beginDay = "";
        String endDay = "";
        SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd");
        List<String> names = new ArrayList<>();
        List<Float> values = new ArrayList<>();
        Map<String, Object> mm = new HashMap<>();
        Map<String, Object> result = new HashMap<>();
        List<Map<String, Object>> list = new ArrayList<>();
        if (dateType.equals("yesterday")) {
            beginDay = formate.format(DateUtil.getYesterday());
            endDay = formate.format(DateUtil.getYesterday());
        } else if (dateType.equals("beforYesterday")) {
            beginDay = formate.format(DateUtil.getYesterday(2));
            endDay = formate.format(DateUtil.getYesterday(2));
        } else if (dateType.equals("week")) {
            List<String> dateArray = DateUtil.getDateArray(7);
            beginDay = dateArray.get(0);
            endDay = dateArray.get(dateArray.size() - 1);
        } else if (dateType.equals("month")) {
            List<String> dateArray = DateUtil.getDateArray(30);
            beginDay = DateUtil.getThisMonth();
            endDay = formate.format(new Date());
        }
        List<Map<String, Object>> maps = reportDaoImpl.selectInventoryClassBycInvCCode(inventoryClass);
        List<ReportForm> storedProcedure = reportDaoImpl.getStoredProcedure(beginDay, endDay, inventoryClass);
        for (int i = 0; i < storedProcedure.size(); i++) {
            if (storedProcedure.get(i).getYield() > 1) {
                names.add(storedProcedure.get(i).getStockName());

                values.add(Float.parseFloat(storedProcedure.get(i).getYield().toString()));
            }
        }
        result.put("categories", names);
        //series
        mm.put("name", maps.get(0).get("cInvCName"));
        mm.put("data", values);
        result.put("series", mm);
        return result;
    }

    @Override
    public List<Map<String, Object>> typesOfAccessories(String dateType, String beginDate, String endDate) {
        List<String> dateArray = getDateArray(dateType, beginDate, endDate);
        List<Map<String, Object>> maps = customSearchDao.typesOfAccessories(dateArray.get(0), dateArray.get(dateArray.size() - 1));
        for (int i = 0; i < maps.size(); i++) {
            if (maps.get(i).get("data") == null) {
                maps.get(i).put("data", 0);
            } else {
                maps.get(i).put("data", Float.parseFloat(maps.get(i).get("data").toString()));
            }
        }
        return maps;
    }
}
